package com.ogilvy.audi.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.ogilvy.audi.service.CircleBulletinManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.ogilvy.audi.model.CircleBulletin;
import com.ogilvy.audi.webapp.action.BaseAction;

import java.util.List;

public class CircleBulletinAction extends BaseAction implements Preparable {
    private CircleBulletinManager circleBulletinManager;
    private List circleBulletins;
    private CircleBulletin circleBulletin;
    private Long bulletinId;
    private String query;
    public ServiceDispatcher serviceDispatcher;

    public void setCircleBulletinManager(CircleBulletinManager circleBulletinManager) {
        this.circleBulletinManager = circleBulletinManager;
    }

    public List getCircleBulletins() {
        return circleBulletins;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleBulletinId = getRequest().getParameter("circleBulletin.bulletinId");
            if (circleBulletinId != null && !circleBulletinId.equals("")) {
                circleBulletin = circleBulletinManager.get(new Long(circleBulletinId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        //circleBulletins = circleBulletinManager.search(query, CircleBulletin.class);
    	log.debug("进入list()");
    	circleBulletins = serviceDispatcher.getCircleBulletinManager().getRecordsByHql("from CircleBulletin",null);
    	log.debug("退出list()");
    	return "list";
    }

    public void setBulletinId(Long bulletinId) {
        this.bulletinId = bulletinId;
    }

    public CircleBulletin getCircleBulletin() {
        return circleBulletin;
    }

    public void setCircleBulletin(CircleBulletin circleBulletin) {
        this.circleBulletin = circleBulletin;
    }

    public String delete() {
        circleBulletinManager.remove(circleBulletin.getBulletinId());
        saveMessage(getText("circleBulletin.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (bulletinId != null) {
            circleBulletin = circleBulletinManager.get(bulletinId);
        } else {
            circleBulletin = new CircleBulletin();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleBulletin.getBulletinId() == null);

        circleBulletinManager.save(circleBulletin);

        String key = (isNew) ? "circleBulletin.added" : "circleBulletin.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}